package com.wondersgroup.fjzlpt.org.controller.rest.jkcs;

import java.util.HashMap;
import java.util.Map;


import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.QysjbDao;
import com.wondersgroup.fjzlpt.org.service.inter.xymb.XymbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.QyxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.ksxx.KsxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgry.YljgryDAO;
import com.wondersgroup.fjzlpt.org.service.inter.fwb.FwbService;
import com.wondersgroup.fjzlpt.org.service.inter.jtqy.JtqyService;
import com.wondersgroup.fjzlpt.org.service.inter.tdgl.TdglService;
import com.wondersgroup.fjzlpt.org.service.inter.wdqyhz.WdqyhzService;
import com.wondersgroup.fjzlpt.org.service.inter.yljgxx.YljgxxService;
import org.thymeleaf.util.StringUtils;

/**
 * 为健康城市项目提供的接口
 * @author gzb   
 * 2017年7月20日
 */
@RestController
@RequestMapping("/jkcs")  //健康城市历史版本
public class JkcsRestController {

	@Autowired
	private TdglService tdglService;
	@Autowired
	private FwbService fwbService;
	@Autowired
	private JtqyService jtqyService;
	@Autowired
	private WdqyhzService wdqyhzService;
	@Autowired
	private YljgxxService yljgxxService;
	@Autowired
	private KsxxDAO ksxxDAO;
	@Autowired
	private YljgryDAO yljgryDAO;
	@Autowired
	private QyxxDAO qyxxDAO;
	@Autowired
	private XymbService xymbService;
	@Autowired
	private QysjbDao qysjbDao;
	
	
	
	/**
	 * 分页查询团队信息到健康城市系统
	 * @param map {XZQHDM=xxx,YLJGDM=xxx,pageSize=xxx,pageNumber=xxx}其中：pageSize和pageNumber必传
	 * @return
	 */
	@RequestMapping(value="/queryTdxxByPageToJkcs", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject queryTdxxByPageToJkcs(@RequestBody() Map map){
		return tdglService.queryTdxxByPageToJkcs(map);
	}

	/**
	 * 查询团队成员信息到健康城市系统
	 * @param map {TDID=xxx}
	 * @return
	 */
	@RequestMapping(value="/queryTdcyListToJkcs", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONArray queryTdcyListToJkcs(@RequestBody() Map map){
		return tdglService.queryTdcylbAll(map);
	}
	
	/**
	 * 查询服务包基本信息和服务包服务列表信息到健康城市系统
	 * @param map {JGXZQHDM=xxx,YLJGID=xxx}
	 * @return
	 */
	@RequestMapping(value="/queryFwbAndFwxxListToJkcs", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONArray queryFwbAndFwxxListToJkcs(@RequestBody() Map map){
		map.put("FWBZT", "1");// 服务包状态
		return fwbService.queryFwbAndFwxxListToJkcs(map);
	}
	
	/**
	 * 保存健康城市系统预签约数据
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/addYqyToJkcs", method = RequestMethod.POST)
	public JSONObject addYqyToJkcs( @RequestBody() Map map) {
		return jtqyService.addYqyToJkcs(map);
	}
	
	/**
	 * 查询签约历史记录到健康城市系统
	 * @param map {ZJHM=xxx}
	 * @return
	 */
	@RequestMapping(value="/queryQyHistoryToJkcs", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject queryQyHistoryByPageToJkcs(@RequestBody()Map map) {
		
		return jtqyService.queryQyHistoryByPageToJkcs(map);
	}
	
	
	/**
	 * 分页查询签约服务列表到健康城市系统
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/queryQyfwByPageToJkcs", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject queryQyfwByPageToJkcs(@RequestBody() Map map){
		//Map<String, String> map = new HashMap<String, String>();
		//map.put("QYXXID", QYXXID);
		return wdqyhzService.queryQyfwByPageToJkcs(map);
	}
	
	/**
	 * 查询已完成服务列表到健康城市
	 * @param map {QYXXID=xxx,FWBID=xxx,FWID=xxx}
	 * @return
	 */
	@RequestMapping(value="/queryYwcfwByPageToJkcs", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject queryYwcfwByPageToJkcs(@RequestBody() Map<String, String> map){
		map.put("FWZT", "1");// 服务状态：已完成
		return wdqyhzService.queryYwcfwByPageToJkcs(map);
	}
	
	
	/**
	 * 查询所有医疗机构信息
	 * @param map 
	 * @return
	 */
	@RequestMapping(value="/queryYljgxxAll", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONArray queryYljgxxAll(@RequestBody() Map<String, String> map){
		return yljgxxService.queryAll(map);
	}
	
	/**
	 * 查询所有科室信息
	 * @param map 
	 * @return
	 */
	@RequestMapping(value="/queryKsxxAll", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONArray queryKsxxAll(@RequestBody() Map<String, String> map){
		return ksxxDAO.queryAll(map);
	}
	
	/**
	 * 查询所有医疗机构人员
	 * @param map 
	 * @return
	 */
	@RequestMapping(value="/queryYljgryAll", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONArray queryYljgryAll(@RequestBody() Map<String, String> map){
		return yljgryDAO.queryAll(map);
	}
	
	/**
	 * 分页查询某个医生所在团队的居民信息列表
	 * @param map {USERID=xxx,QYLX=xxx,pageSize=xxx,pageNumber=xxx} 注： USERID:医生id,QYLX:签约类型
	 * @return
	 */
	@RequestMapping(value="/queryJmxxByPage", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject queryJmxxByPage(@RequestBody() Map<String, String> map){
		return qyxxDAO.queryPageForGzt(map);
	}
	
	/**
	 * 判断居民是否已经存在签约信息(包括预签约)
	 * @param map {ZJHM=xxx}
	 * @return
	 */
	@RequestMapping(value="/isExist", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject isExist(@RequestBody() Map<String, String> map){
		return jtqyService.isExist(map);
	}

	/**
	 * 审核预签约记录（同意签约/拒绝签约）
	 * @param map {QYXXID=xxx,type=xxx}  注：type='agree' || type='noagree'
	 * @return
	 */
	@RequestMapping(value="/checkYqyData", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject checkYqyData(@RequestBody() Map<String, String> map){
		return jtqyService.checkYqyData(map);
	}

	/**
	 * 完成服务
	 * @param map {FPFWID:"xxx","SJFWSJ":"xxx","BZ":"xxx",USERID:"xxx"}
	 * @return
	 */
	@RequestMapping(value="/finishTask", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject finishTask(@RequestBody() Map<String, String> map){
		JSONArray fpfwArr = new JSONArray();
		map.put("FWZT","1");//状态完成
		fpfwArr.add(map);

		//封装数据： {fpfwArr:[{FPFWID:"xxx","SJFWSJ":"xxx","BZ":"xxx","ZT":"1"}],USERID:"xxx"}
		Map insertMap = new HashMap();
		insertMap.put("type","update");
		insertMap.put("USERID", map.get("USERID")); //完成任务的用户
		insertMap.put("fpfwArr", fpfwArr.toString());
		return wdqyhzService.save(insertMap);
	}
	/**
	 * 查询家庭签约协议模板
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/queryJtqyXymb", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject queryJtqyXymb(@RequestBody() Map<String, String> map){
		String action = map.remove("action");
		if("XYMB".equals(action)){
			//获取空白协议
			return this.getXymb(map.get("QYXYMBID"));
		}else if("QYXYNR".equals(action)){
			//获取 空白协议 + 预签约数据
			map.put("QYXXIDS" , map.get("QYXXID"));
			JSONObject qyxynr = qyxxDAO.queryAllOnView(map).getJSONObject(0);
			String XYH = qysjbDao.queryMaxXyh(qyxynr.getString("XZQHDM"));
			qyxynr.put("XYH" , XYH);
			JSONObject xymb = this.getXymb(map.get("QYXYMBID"));

			qyxynr.put("XYMB" , xymb);
			return qyxynr;
		}else{
			//获取最近协议
			map.put("SJLX" , "01");
			return jtqyService.queryQyxyToJkcs(map);
		}

	}

	/**
	 * 根据XYMBID参数获取协议
	 * @param XYMBID
	 * @return
	 */
	private JSONObject getXymb(String XYMBID){
		if(StringUtils.isEmpty(XYMBID)){
			return xymbService.getJtqyXymb(); //获取正在启用
		}else{
			return xymbService.queryXymb(XYMBID); //获取指定的--德清(一个医院一个协议)
		}
	}

	/**
	 * 健康城市App更新签约信息与协议
	 * @param map 签约协议相关数据
	 * @return
	 */
	@RequestMapping(value="/submitQyqrToJkcs", method = {RequestMethod.POST,RequestMethod.GET})
	public JSONObject submitQyqrToJkcs(@RequestBody() Map<String, String> map){
		return jtqyService.zsQyxxUpdateForJkcs(map);
	}

	/**
	 * 根据医生ID查询该医生签约的患者总数
	 * @param map {USERID=xxx}
	 * @return
	 */
	@RequestMapping(value="/queryQyzsToJkcs", method = {RequestMethod.POST})
	public String queryQyzsToJkcs(@RequestBody() Map map){
		return jtqyService.queryQyzsToJkcs(map);
	}

}

